package org.jdsm.flow.gxt.users.client.widgets.group;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.data.client.loader.HttpProxy;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoader;
import com.sencha.gxt.data.shared.writer.JsonWriter;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import org.jdsm.flow.gxt.users.client.UserAutoBeanFactory;

import java.util.ArrayList;
import java.util.List;

import static org.jdsm.flow.gxt.users.client.UserAutoBeanFactory.*;

/**
 * Created by IntelliJ IDEA.
 * User: mikom
 * Date: 7/5/12
 * Time: 7:00 PM
 */
public class GroupsGrid implements IsWidget {

    PagingLoader<PagingLoadConfig, GroupPagingLoadResult> loader;

    @Override
    public Widget asWidget() {

        UserAutoBeanFactory factory = GWT.create(UserAutoBeanFactory.class);

        GroupDataRecordJsonReader reader =
                new GroupDataRecordJsonReader(factory);

        String path = "data/groups.json";

        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
        HttpProxy<PagingLoadConfig> proxy = new HttpProxy<PagingLoadConfig>(builder);
        JsonWriter<PagingLoadConfig> writer = new JsonWriter<PagingLoadConfig>(factory, PagingLoadConfig.class);
        proxy.setWriter(writer);

        loader = new PagingLoader<PagingLoadConfig, GroupPagingLoadResult>(proxy, reader);
        loader.useLoadConfig(factory.pagingLoadConfig().as());
        loader.setLimit(10);
        loader.setRemoteSort(true);
        GroupProperties props = GWT.create(GroupProperties.class);

        ListStore<Group> store = new ListStore<Group>(props.key());
        loader.addLoadHandler(new LoadResultListStoreBinding<PagingLoadConfig,
                Group,
                GroupPagingLoadResult>(store));

        ColumnConfig<Group, String> columnName = new ColumnConfig<Group, String>(props.name(), 200, "Name");
        ColumnConfig<Group, Integer> columnPermission = new ColumnConfig<Group, Integer>(props.permission(), 200, "permission");

        List<ColumnConfig<Group, ?>> l = new ArrayList<ColumnConfig<Group,?>>();
        l.add(columnName);
        l.add(columnPermission);
        ColumnModel<Group> cm = new ColumnModel<Group>(l);

        Grid<Group> grid = new Grid<Group>(store, cm);
        grid.getView().setForceFit(true);
        grid.setLoader(loader);
        grid.setLoadMask(true);
        grid.setBorders(true);
        grid.getView().setEmptyText("No result found");


        grid.addAttachHandler(new AttachEvent.Handler() {
            @Override
            public void onAttachOrDetach(AttachEvent event) {
                Timer t = new Timer(){
                    @Override
                    public void run() {
                        loader.load();
                    }
                };
                t.schedule(1000);
            }
        });

        return grid;
    }

    public PagingLoader<PagingLoadConfig, GroupPagingLoadResult> getLoader() {
        return loader;
    }
}
